####### SCRIPT FOR: 
####### I Don't Like It Unless It's for Me: 
####### Experimental Test of Voters' Perception of Pork-Barrel Politics

rm(list=ls())
gc()

setwd("C:/Users/miros/Desktop/Research/Experimental Test of Voters Perception of Pork-barrel Politics")




# DATA: Getting it ready --------------------------------------------------

#Loading data for SVK
library(haven)
exp_data_svk <- read_sav("exp_data_svk.sav")
exp_data_svk$CNTRY <- as.factor("Slovakia")

#Loading data for CZE
exp_data_cze <- read_sav("exp_data_cze.sav")
exp_data_cze$CNTRY <- as.factor("Czech Republic")
#Recoding separate variables for different versions into a single variable
exp_data_cze$Q1 <- ifelse(is.na(exp_data_cze$Q1AB), exp_data_cze$Q1CD, exp_data_cze$Q1AB)
exp_data_cze$Q1CD <- NULL
exp_data_cze$Q1AB <- NULL
exp_data_cze$Q2 <- ifelse(is.na(exp_data_cze$Q2AB), exp_data_cze$Q2CD, exp_data_cze$Q2AB)
exp_data_cze$Q2CD <- NULL
exp_data_cze$Q2AB <- NULL
exp_data_cze$Q3 <- ifelse(is.na(exp_data_cze$Q3AB), exp_data_cze$Q3CD, exp_data_cze$Q3AB)
exp_data_cze$Q3CD <- NULL
exp_data_cze$Q3AB <- NULL

#Harmonizing the age groups in both datasets
exp_data_cze$AGE <- NA
exp_data_cze$AGE[exp_data_cze$AGE.C >= 18 & exp_data_cze$AGE.C <= 29] <- 1
exp_data_cze$AGE[exp_data_cze$AGE.C >= 30 & exp_data_cze$AGE.C <= 39] <- 2
exp_data_cze$AGE[exp_data_cze$AGE.C >= 40 & exp_data_cze$AGE.C <= 49] <- 3
exp_data_cze$AGE[exp_data_cze$AGE.C >= 50 & exp_data_cze$AGE.C <= 59] <- 4
exp_data_cze$AGE[exp_data_cze$AGE.C >= 60]                            <- 5
exp_data_cze$AGE <- as.factor(exp_data_cze$AGE)
is.factor(exp_data_cze$AGE)

#Harmonizing the size of residency in both datasets
exp_data_svk$SIZE <- exp_data_svk$SIZE-1

#Harmonizing the both data files before merge
exp_data_cze$X_TIME <- NULL
exp_data_cze$X_DUR <- NULL
exp_data_cze$AGE.C <- NULL
exp_data_svk$NAT <- NULL

#Check whether structure is the same
names(exp_data_cze)
names(exp_data_svk)

#Merging data
library(plyr)
exp_data <- rbind.fill(exp_data_svk, exp_data_cze)
#View(exp_data)
summary(exp_data)
rm(exp_data_svk, exp_data_cze)

#Number of participants in groups
table(exp_data$CNTRY, exp_data$SKUPINA)

#Adding dummies
exp_data$BENEFIT <- NA
exp_data$BENEFIT[exp_data$SKUPINA == 1] <- "Benefit"
exp_data$BENEFIT[exp_data$SKUPINA == 2] <- "Benefit"
exp_data$BENEFIT[exp_data$SKUPINA == 3] <- "No benefit"
exp_data$BENEFIT[exp_data$SKUPINA == 4] <- "No benefit"
exp_data$BENEFIT <- factor(exp_data$BENEFIT, levels = c("No benefit", "Benefit"))
is.factor(exp_data$BENEFIT)

exp_data$FAIR <- NA
exp_data$FAIR[exp_data$SKUPINA == 1] <- "Fair"
exp_data$FAIR[exp_data$SKUPINA == 2] <- "Unfair"
exp_data$FAIR[exp_data$SKUPINA == 3] <- "Fair"
exp_data$FAIR[exp_data$SKUPINA == 4] <- "Unfair"
exp_data$FAIR <- factor(exp_data$FAIR, levels = c("Unfair", "Fair"))
is.factor(exp_data$FAIR)

#Renaming variables
exp_data$Q1TRUST <- exp_data$Q1
exp_data$Q1 <- NULL
exp_data$Q2VOTE <- exp_data$Q2
exp_data$Q2 <- NULL
exp_data$Q3SUPPORT <- exp_data$Q3
exp_data$Q3 <- NULL

#Defining factors
#exp_data$CNTRY <- factor(exp_data$CNTRY, levels = c("Czech Republic", "Slovakia"))

exp_data$FEMALE[exp_data$SEX == 1] <- "Male"
exp_data$FEMALE[exp_data$SEX == 2] <- "Female"
exp_data$FEMALE <- factor(exp_data$FEMALE, levels = c("Male", "Female"))

exp_data$AGE <- as.character(exp_data$AGE)
exp_data$AGE[exp_data$AGE == 1] <- "18-29"
exp_data$AGE[exp_data$AGE == 2] <- "30-39"
exp_data$AGE[exp_data$AGE == 3] <- "40-49"
exp_data$AGE[exp_data$AGE == 4] <- "50-59"
exp_data$AGE[exp_data$AGE == 5] <- "60+"
exp_data$AGE <- factor(exp_data$AGE, levels = c("18-29", "30-39","40-49" ,"50-59", "60+"))

exp_data$SIZE[exp_data$SIZE == 1] <- "Less than 4 999"
exp_data$SIZE[exp_data$SIZE == 2] <- "5 000 - 19 999"
exp_data$SIZE[exp_data$SIZE == 3] <- "20 000 - 99 999"
exp_data$SIZE[exp_data$SIZE == 4] <- "More than 100 000"
exp_data$SIZE <- factor(exp_data$SIZE, levels = c("Less than 4 999", "5 000 - 19 999", "20 000 - 99 999" ,"More than 100 000"))

exp_data$EDU <- as.character(exp_data$EDU)
exp_data$EDU[exp_data$EDU == 1] <- "Primary School"
exp_data$EDU[exp_data$EDU == 2] <- "Secondary school without graduation examination"
exp_data$EDU[exp_data$EDU == 3] <- "Secondary school with graduation examination"
exp_data$EDU[exp_data$EDU == 4] <- "College/University"
exp_data$EDU <- factor(exp_data$EDU, levels = c("Primary School", "Secondary school without graduation examination", "Secondary school with graduation examination" ,"College/University"))



# PART 1: Trust in decision-maker --------------------------------

#regression model
m.1a <- lm(Q1TRUST ~ BENEFIT + FAIR, data = exp_data)
summary(m.1a)

m.1b <- lm(Q1TRUST ~ BENEFIT + FAIR + CNTRY, data = exp_data)
summary(m.1b)

m.1c <- lm(Q1TRUST ~ BENEFIT*FAIR + CNTRY, data = exp_data)
summary(m.1c)

m.1d <- lm(Q1TRUST ~ BENEFIT*FAIR*CNTRY, data = exp_data)
summary(m.1d)

m.1e <- lm(Q1TRUST ~ BENEFIT*FAIR*CNTRY + FEMALE + AGE + SIZE + EDU, data = exp_data)
summary(m.1e)


#Models' output
library(stargazer)
stargazer(m.1a,m.1b, m.1c, m.1d, m.1e,
          type="html", out="models_1_trust.htm", single.row = TRUE, no.space = TRUE)


#Visualisation
library(ggplot2)
library(sjPlot)
library(sjmisc)
library(grid)



#Figure 1: Overall
plot_model(m.1c, type = "pred",
           terms = c("BENEFIT", "FAIR"),
           legend.title = "Distribution: ",
           colors = "gs") +
  coord_cartesian(xlim = c(0.65, 2.35), ylim = c(0, 10), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10)) +
  labs(title = NULL, x = NULL, y = "Trust in the decision-maker") +
  theme(legend.position="right",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.key.width = unit(1, "cm"),
        legend.text.align = 0,
        legend.title.align = 0.5,
        legend.box.just = "top",
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray95"),
        panel.grid.minor.y = element_line(colour = "gray95"),
        panel.background = element_blank(),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5),"cm"))

ggsave(filename = "Fig1 - trust - overall.png", width = 13, height = 10, units = "cm", dpi = 600) 



#Figure A1: Countries
plot_model(m.1d, type = "pred",
           terms = c("BENEFIT", "FAIR", "CNTRY"),
           legend.title = "Distribution: ",
           colors = "gs") +
  coord_cartesian(xlim = c(0.65, 2.35), ylim = c(0, 10), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10)) +
  labs(title = NULL, x = NULL, y = "Trust in the decision-maker") +
  theme(legend.position="right",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.key.width = unit(1, "cm"),
        legend.text.align = 0,
        legend.title.align = 0.5,
        legend.box.just = "top",
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray95"),
        panel.grid.minor.y = element_line(colour = "gray95"),
        panel.background = element_blank(),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5),"cm"))

ggsave(filename = "FigA1 - trust - countries.png", width = 20, height = 10, units = "cm", dpi = 600)  

rm(m.1a,m.1b, m.1c, m.1d, m.1e)




# PART 2: Willingnes to vote ----------------------------------------------

#regression model
m.2a <- lm(Q2VOTE ~ BENEFIT + FAIR, data = exp_data)
summary(m.2a)

m.2b <- lm(Q2VOTE ~ BENEFIT + FAIR + CNTRY, data = exp_data)
summary(m.2b)

m.2c <- lm(Q2VOTE ~ BENEFIT*FAIR + CNTRY, data = exp_data)
summary(m.2c)

m.2d <- lm(Q2VOTE ~ BENEFIT*FAIR*CNTRY, data = exp_data)
summary(m.2d)

m.2e <- lm(Q2VOTE ~ BENEFIT*FAIR*CNTRY + FEMALE + AGE + SIZE + EDU, data = exp_data)
summary(m.2e)

#Models' output
library(stargazer)
stargazer(m.2a,m.2b, m.2c, m.2d, m.2e,
          type="html", out="models_2_vote.htm", single.row = TRUE, no.space = TRUE)


#Visualisation
library(ggplot2)
library(sjPlot)
library(sjmisc)



#Figure 2: Overall
plot_model(m.2c, type = "pred",
           terms = c("BENEFIT", "FAIR"),
           legend.title = "Distribution: ",
           colors = "gs") +
  coord_cartesian(xlim = c(0.65, 2.35), ylim = c(0, 10), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10)) +
  labs(title = NULL, x = NULL, y = "Willingness to vote for the decision-maker") +
  theme(legend.position="right",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.key.width = unit(1, "cm"),
        legend.text.align = 0,
        legend.title.align = 0.5,
        legend.box.just = "top",
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray95"),
        panel.grid.minor.y = element_line(colour = "gray95"),
        panel.background = element_blank(),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5),"cm"))

ggsave(filename = "Fig2 - vote - overall.png", width = 13, height = 10, units = "cm", dpi = 600)


#Figure A2: Countries
plot_model(m.2d, type = "pred",
           terms = c("BENEFIT", "FAIR", "CNTRY"),
           legend.title = "Distribution: ",
           colors = "gs") +
  coord_cartesian(xlim = c(0.65, 2.35), ylim = c(0, 10), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10)) +
  labs(title = NULL, x = NULL, y = "Willingness to vote for the decision-maker") +
  theme(legend.position="right",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.key.width = unit(1, "cm"),
        legend.text.align = 0,
        legend.title.align = 0.5,
        legend.box.just = "top",
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray95"),
        panel.grid.minor.y = element_line(colour = "gray95"),
        panel.background = element_blank(),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5),"cm"))

ggsave(filename = "FigA2 - vote - countries.png", width = 20, height = 10, units = "cm", dpi = 600)  

rm(m.2a,m.2b, m.2c, m.2d, m.2e)



# PART 3: Support for the policy ------------------------------------------

#regression model
m.3a <- lm(Q3SUPPORT ~ BENEFIT + FAIR, data = exp_data)
summary(m.3a)

m.3b <- lm(Q3SUPPORT ~ BENEFIT + FAIR + CNTRY, data = exp_data)
summary(m.3b)

m.3c <- lm(Q3SUPPORT ~ BENEFIT*FAIR + CNTRY, data = exp_data)
summary(m.3c)

m.3d <- lm(Q3SUPPORT ~ BENEFIT*FAIR*CNTRY, data = exp_data)
summary(m.3d)

m.3e <- lm(Q3SUPPORT ~ BENEFIT*FAIR*CNTRY + FEMALE + AGE + SIZE + EDU, data = exp_data)
summary(m.3e)

#Models' output
library(stargazer)
stargazer(m.3a,m.3b, m.3c, m.3d, m.3e,
          type="html", out="models_3_support.htm", single.row = TRUE, no.space = TRUE)


#visualisation
library(ggplot2)
library(sjPlot)
library(sjmisc)



#Figure 3: Overall
plot_model(m.3c, type = "pred",
           terms = c("BENEFIT", "FAIR"),
           legend.title = "Distribution: ",
           colors = "gs") +
  coord_cartesian(xlim = c(0.65, 2.35), ylim = c(0, 10), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10)) +
  labs(title = NULL, x = NULL, y = "Support for the policy") +
  theme(legend.position="right",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.key.width = unit(1, "cm"),
        legend.text.align = 0,
        legend.title.align = 0.5,
        legend.box.just = "top",
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray95"),
        panel.grid.minor.y = element_line(colour = "gray95"),
        panel.background = element_blank(),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5),"cm"))

ggsave(filename = "Fig3 - support - overall.png", width = 13, height = 10, units = "cm", dpi = 600)



#Figure A3: Countries
plot_model(m.3d, type = "pred",
           terms = c("BENEFIT", "FAIR", "CNTRY"),
           legend.title = "Distribution: ",
           colors = "gs") +
  coord_cartesian(xlim = c(0.65, 2.35), ylim = c(0, 10), expand = FALSE) +
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10)) +
  labs(title = NULL, x = NULL, y = "Support for the policy") +
  theme(legend.position="right",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.key.width = unit(1, "cm"),
        legend.text.align = 0,
        legend.title.align = 0.5,
        legend.box.just = "top",
        panel.border = element_rect(fill = "transparent"),
        strip.background = element_rect(colour = "black"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour = "gray95"),
        panel.grid.minor.y = element_line(colour = "gray95"),
        panel.background = element_blank(),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5),"cm"))

ggsave(filename = "FigA3 - support - countries.png", width = 20, height = 10, units = "cm", dpi = 600)  


rm(m.3a,m.3b, m.3c, m.3d, m.3e)


